/*
 * Copyright 2014 Cungmua, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package cungmua.vn.kiosk.adapter;

import java.util.ArrayList;
import java.util.List;

import com.androidquery.AQuery;

import cungmua.vn.kiosk.R;
import cungmua.vn.kiosk.model.DonHang;
import cungmua.vn.kiosk.model.SKU;
import cungmua.vn.kiosk.utils.Constant;
import cungmua.vn.kiosk.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author kelvin.nguyen
 *
 */
public class ReportAdapter extends BaseAdapter {
	Activity mContext;
	private List<SKU> skus = new ArrayList<SKU>();
	private SKU sku;

	public ReportAdapter(Activity c, List<SKU> skus) {
		mContext = c;
		this.skus = skus;

	}

	@Override
	public int getCount() {
		int size = (skus != null) ? skus.size() : 0;
		return size;
	}

	@Override
	public Object getItem(int position) {
		SKU item = (skus != null) ? skus.get(position) : null;
		return item;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void addItem(List<SKU> items) {
		int size = items.size();
		for (int i = 0; i < size; i++) {
			SKU item = items.get(i);
			skus.add(item);
		}
		this.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		sku= skus.get(position);

		LayoutInflater inflator = mContext.getLayoutInflater();
		if (convertView == null) {
			holder = new ViewHolder();

			convertView = inflator.inflate(R.layout.row_thongke, null);
			holder.txt_sku_id=(TextView)convertView.findViewById(R.id.txt_sku_id);
			
			holder.root_row_report=(LinearLayout)convertView.findViewById(R.id.root_row_report);
			
			holder.txt_sku_name=(TextView)convertView.findViewById(R.id.txt_sku_name);
			
			holder.txt_sku_id=(TextView)convertView.findViewById(R.id.txt_sku_id);
			holder.txt_sku_price=(TextView)convertView.findViewById(R.id.txt_sku_price);
			holder.txt_sku_quantity=(TextView)convertView.findViewById(R.id.txt_sku_quantity);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		int bg=position%2==0?mContext.getResources().getColor(R.color.white):mContext.getResources().getColor(R.color.color_bg);
		
		holder.root_row_report.setBackgroundColor(bg);

		holder.txt_sku_name.setText(sku.getSkuName());
		
		holder.txt_sku_id.setText(sku.getProductType()==1?Constant.TYPE_VOUCHER:Constant.TYPE_PRODUCT+sku.getSkuId());
		holder.txt_sku_price.setText(Utils.priceWithoutDecimal(sku.getPrice())+Constant.PREFIX_CURRENCY);
		holder.txt_sku_quantity.setText(String.valueOf(sku.getSku_Quantity_Delivery()));
		
		return convertView;
	}

	public static class ViewHolder {
		TextView txt_sku_name;
		TextView txt_sku_id;
		TextView txt_sku_price;
		TextView txt_sku_quantity;
		LinearLayout root_row_report;
		
	}

}
